//
//  WKChangePasswordController.m
//  WeKeTang
//
//  Created by ichina on 2017/9/14.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKChangePasswordController.h"
#import "NSString+KZExtension.h"
#import "WKUniversal.h"
#import "WKNetworkTools.h"
#import "WKLoginHandel.h"

@interface WKChangePasswordController ()

@property (weak, nonatomic) IBOutlet UITextField *TFCurrentPsd;

@property (weak, nonatomic) IBOutlet UITextField *TFNewPsd;

@property (weak, nonatomic) IBOutlet UIButton *btnComplete;


@end

@implementation WKChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navItem.title = @"修改密码";
    
    [self setupUI];
}
- (void)setupUI{
    
    self.navgationBar.barTintColor = RGBColor(40, 188, 203);
    
    self.statusbar.backgroundColor = RGBColor(40, 188, 203);
    
    [self middleTitleColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = RGBColor(245, 245, 245);
    
    [self setLeftButtonWithImage:@"icon_return_white" highlightedImage:@"icon_return_white" target:self action:@selector(backAction)];
    
    self.btnComplete.layer.cornerRadius = 2;
    
    self.btnComplete.layer.masksToBounds = YES;
    
    UIView *leftView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 47)];
    leftView1.contentMode = UIViewContentModeCenter;
    self.TFCurrentPsd.leftView = leftView1;
    self.TFCurrentPsd.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *leftView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 47)];
    leftView2.contentMode = UIViewContentModeCenter;
    self.TFNewPsd.leftView = leftView2;
    self.TFNewPsd.leftViewMode = UITextFieldViewModeAlways;
    
    self.TFCurrentPsd.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.TFNewPsd.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    

}
- (IBAction)btnCompleteAction:(UIButton *)sender {
    
    if (StringIsNullOrEmpty(self.TFCurrentPsd.text)) {
        
        [self showCustomAlertMessage:@"请输当前入密码"];
        return;
    }
    NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    NSLog(@"当前密码：%@",password);
    NSLog(@"新密码：%@",self.TFCurrentPsd.text);
    if (![password isEqualToString:self.TFCurrentPsd.text]) {
        
        [self showCustomAlertMessage:@"当前密码输入错误!"];
        return;
    }
    if (StringIsNullOrEmpty(self.TFNewPsd.text)) {
        
        [self showCustomAlertMessage:@"请输入新密码"];
        return;
    }
    if ([self.TFCurrentPsd.text length]<6||[self.TFCurrentPsd.text length]>18 ||[self.TFNewPsd.text length]<6|| [self.TFNewPsd.text length]>18) {
        [self showCustomAlertMessage:@"密码长度不合法"];
        
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSString *userid = [WKLoginHandel getLoginDataWithString:@"userid"];
    NSString *mac = [WKUniversal macaddress];
    NSString *phpsessid = [WKLoginHandel getLoginDataWithString:@"SESSID"];
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:@"updateinfo" forKey:@"commandid"];
    [para setObject:self.TFNewPsd.text forKey:@"password"];
    [para setObject:userid forKey:@"userid"];
    [para setObject:phpsessid forKey:@"phpsessid"];
    [para setObject:mac forKey:@"mac"];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[WKNetworkTools shareManger]setHttpMethod:@"POST"];
        
        [[WKNetworkTools shareManger]RunNoneRequest:para path:kServerDomain tip:nil completionHandler:^(id responseObject) {
            
        } errorHandler:^(NSError *error) {
            [weakSelf showCustomAlertMessage:@"网络错误"];
        } finishHandler:^(id responseObject) {
            NSDictionary *dic = [responseObject mj_JSONObject];
            NSDictionary *headDict = dic[@"head"];
//            NSDictionary *bodyDict = dic[@"body"];
            NSString *statuscode = headDict[@"statuscode"];
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([statuscode intValue] == 100) {
                    [weakSelf showCustomAlertMessage:@"修改成功"];
                }else{
                    [weakSelf showCustomAlertMessage:@"修改失败"];
                }
            });
            
            NSLog(@"播放接口:%@",dic);
        }];
    });

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view resignFirstResponder];
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
